package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatGift;
import com.happy.cat.project.system.service.ICatGiftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 礼品Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/gift" )
@Api("礼品 API")
public class CatGiftController extends BaseController {

    private final ICatGiftService iCatGiftService;

    /**
     * 查询礼品列表
     */
    @ApiOperation(value = "查询礼品列表",notes = "查询礼品列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:gift:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatGift catGift)
    {
        startPage();
        LambdaQueryWrapper<CatGift> lqw = new LambdaQueryWrapper<CatGift>();
        if (StringUtils.isNotBlank(catGift.getGiftType())){
            lqw.eq(CatGift::getGiftType ,catGift.getGiftType());
        }
        if (StringUtils.isNotBlank(catGift.getName())){
            lqw.like(CatGift::getName ,catGift.getName());
        }
        if (StringUtils.isNotBlank(catGift.getGiftImg())){
            lqw.eq(CatGift::getGiftImg ,catGift.getGiftImg());
        }
        if (catGift.getFoodNumber() != null){
            lqw.eq(CatGift::getFoodNumber ,catGift.getFoodNumber());
        }
        if (catGift.getPosition() != null){
            lqw.eq(CatGift::getPosition ,catGift.getPosition());
        }
        if (catGift.getLevel() != null){
            lqw.eq(CatGift::getLevel ,catGift.getLevel());
        }
        if (catGift.getExperience() != null){
            lqw.eq(CatGift::getExperience ,catGift.getExperience());
        }
        if (catGift.getIntervalTime() != null){
            lqw.eq(CatGift::getIntervalTime ,catGift.getIntervalTime());
        }
        if (catGift.getSort() != null){
            lqw.eq(CatGift::getSort ,catGift.getSort());
        }
        if (catGift.getStatus() != null){
            lqw.eq(CatGift::getStatus ,catGift.getStatus());
        }
        List<CatGift> list = iCatGiftService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出礼品列表
     */
    @ApiOperation(value = "导出礼品列表",notes = "导出礼品列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:gift:export')" )
    @Log(title = "礼品" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatGift catGift) {
        LambdaQueryWrapper<CatGift> lqw = new LambdaQueryWrapper<CatGift>(catGift);
        List<CatGift> list = iCatGiftService.list(lqw);
        ExcelUtil<CatGift> util = new ExcelUtil<CatGift>(CatGift. class);
        return util.exportExcel(list, "gift" );
    }

    /**
     * 获取礼品详细信息
     */
    @ApiOperation(value = "获取礼品详细信息",notes = "获取礼品详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:gift:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatGiftService.getById(id));
    }

    /**
     * 新增礼品
     */
    @ApiOperation(value = "新增礼品",notes = "新增礼品",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:gift:add')" )
    @Log(title = "礼品" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatGift catGift) {
        catGift.setCreateTime(new Date());
        catGift.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGiftService.save(catGift) ? 1 : 0);
    }

    /**
     * 修改礼品
     */
    @ApiOperation(value = "修改礼品",notes = "修改礼品",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:gift:edit')" )
    @Log(title = "礼品" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatGift catGift) {
        catGift.setCreateTime(new Date());
        catGift.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGiftService.updateById(catGift) ? 1 : 0);
    }

    /**
     * 删除礼品
     */
    @ApiOperation(value = "删除礼品",notes = "删除礼品",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:gift:remove')" )
    @Log(title = "礼品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatGiftService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
